<template>
  <div class="app-container">
    <div class="search">
      <p class="float-left mr-10 mb-10" style="width: 200px">
        <el-input v-model="searchParams.name" placeholder="输入名称"></el-input>
      </p>
      <p class="float-left mr-10 mb-10">
        <el-button type="primary" @click="searchDept(true)">搜索</el-button>
        <el-button type="primary" @click="resetDept">重置</el-button>
      </p>
    </div>
    <div class="my-10">
      <el-button type="primary" @click="addDept">新增社区</el-button>
    </div>
    <bdTable :tableHeader="tableHeader" :tableData="tableData" class="mb-10" />
    <bd-page
      :pageSize="pages"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></bd-page>
    <addDeptAndEditDept ref="deptInfo" @update:visible="searchDept(true)"/>
  </div>
</template>
<script>
import { getDept, switchDept, deleteDept } from '@/api/dept'
import addDeptAndEditDept from './addDeptAndEditDept'
export default {
  name: '',
  components: {
    addDeptAndEditDept
  },
  data () {
    return {
      searchParams: {
        name: null
      },
      tableHeader: {
        type: 'selection',
        header: [
          {
            prop: 'name',
            label: '社区名称'
          },
          {
            prop: 'status',
            label: '状态',
            type: 'switch',
            activeValue: 1,
            inactiveValue: 0,
            handler: this.changeStatus
          },
          {
            prop: 'createTime',
            label: '创建日期'
          },
          {
            label: '操作',
            type: 'button',
            width: 150,
            btn: [
              {
                id: 1,
                name: '编辑',
                handler: this.handleEdit
              },
              {
                id: 2,
                name: '删除',
                type: 'danger',
                handler: this.handleDelete
              }
            ]
          }
        ]
      },
      tableData: [],
      // 分页
      pages: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  // 计算属性
  computed: {},
  watch: {},
  mounted () {
    this.searchDept()
  },
  methods: {
    async searchDept (clearPage = false) {
      if (clearPage) {
        this.pages.page = 1
        this.pages.pageSize = 10
      }
      const params = {
        ...this.pages,
        ...this.searchParams
      }
      const { total, list } = await getDept(params)
      this.tableData = list
      this.total = total
    },
    // 重置
    resetDept () {
      this.searchParams.name = ''
      this.searchDept(true)
    },
    // 新增社区
    addDept () {
      this.$refs.deptInfo.isShow()
    },
    // 编辑社区
    handleEdit (e, row) {
      this.$refs.deptInfo.isShow(row, true)
    },
    // 删除社区
    handleDelete (e, row) {
      this.$confirm('此操作将删除社区, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await deleteDept({ id: row.id })
        this.searchDept(true)
      }).catch(() => {
      })
    },
    // 切换状态
    changeStatus (row) {
      this.$confirm(`此操作将${row.status ? '启用' : '禁用'}社区, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const status = row.status ? 1 : 0
        switchDept({ id: row.id, status })
      }).catch(() => {
        row.status = row.status ? 0 : 1
      })
    },
    // 条数
    handleSizeChange (val) {
      this.pages.pageSize = val
      this.searchDept()
    },
    // 页数
    handleCurrentChange (val) {
      this.pages.page = val
      this.searchDept()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
